class CourseController < ApplicationController
  layout 'main'
  after_filter :post_log
  before_filter :admin?, :only=>[:delete, :edit, :create]

  def new
    @course_categories= CourseCategory.all.collect {|p| [ p.name, p.id ]}
  end
  def create
    course = Course.new(params[:course])
    if(params[:course][:image]!=nil && params[:course][:image]!='')
      filename = old_save_file(params[:course][:image])
      course.image = filename
    end
    course.save
    flash[:notice] = 'เพิ่มรายวิชาเป็นที่เรียบร้อยแล้ว'
    redirect_to :action => 'admin'
  end
  def admin
    @courses= Course.find :all, :order=>:id
  end
  def material
    @course= Course.find(params[:id])
  end
  def edit
    @course= Course.find params[:id]
  end
  def update
    #get old filename
    old_filename = Course.find(params[:course][:id]).image
    course = Course.update(params[:course][:id], params[:course])
    if(params[:course][:image]!=nil && params[:course][:image]!='')
      filename = old_save_file(params[:course][:image])
      course.image = filename
    else
      course.image = old_filename
    end
    course.save
    flash[:notice] = 'แก้ไขรายวิชาเป็นที่เรียบร้อยแล้ว'
    redirect_to :action => 'admin'
  end
  def delete
    course= Course.find params[:id]
    course.destroy
    redirect_to :action=>:admin
  end
  def content
    @course= Course.find params[:id]
    @videos= Video.find(:all, :conditions=>["course_id=?", @course.id], :order=>"id")
    @materials= Material.find(:all, :conditions=>["course_id=?", @course.id], :order=>"id")
  end
  def download
    @material = Material.find params[:id]
  end

  # materials
  def new_material
    @material= Material.new :course_id=>params[:course]
  end
  def edit_material
    @material= Material.find params[:id]
  end
  def update_material
    #get old filename
    old_filename = Material.find(params[:material][:id]).filename
    size= params[:material][:filename].size/1e6
    material = Material.update(params[:material][:id], params[:material])
    if(params[:material][:filename]!=nil && params[:material][:filename]!='')
      filename = save_file(params[:material][:filename])
      material.filename = filename
    else
      material.filename = old_filename
    end
    material.size= size
    material.save
    flash[:notice] = 'แก้ไขสื่อการสอนเป็นที่เรียบร้อยแล้ว'
    redirect_to :action=>'material', :id=>material.course_id
  end
  def delete_material
    material= Material.find params[:id]
    flash[:notice]= "ลบสื่อการสอน #{material.name} เรียบร้อยแล้ว"
    course= material.course_id
    material.destroy
    redirect_to :action=>'material', :id=>course
  end
  def create_material
    material = Material.new(params[:material])
    size= params[:material][:filename].size/1e6
    if(params[:material][:filename]!=nil && params[:material][:filename]!='')
      filename = save_file(params[:material][:filename])
      material.filename = filename
    end
    material.size= size
    material.save
    flash[:notice] = 'เพิ่มสื่อการสอนเป็นที่เรียบร้อยแล้ว'
    redirect_to :action => 'material', :id=>material.course_id
  end

  #video
  def video_stream
    @video= Video.find params[:id]
  end
  def video
    @course= Course.find params[:id]
    @videos= Video.find_all_by_course_id params[:id]
  end
  def new_video
    @video= Video.new :course_id=>params[:course]
    @submit_message = 'เพิ่มวิดีทัศน์'
  end
  def create_video
    @video= Video.new params[:video]
    @video.save
    Video.last.move_to_top
    flash[:notice] = 'เพิ่มวิดีทัศน์เป็นที่เรียบร้อยแล้ว'
    redirect_to :action => 'video', :id=>@video.course_id
  end
  def sort
    params[:videos].each_with_index do |id, index|
      Video.update_all(['position=?', index+1], ['id=?', id])
    end
    render :nothing => true
  end
  def edit_video
    @video= Video.find params[:id]
    @submit_message = 'แก้ไข'
  end
  def update_video
    video = Video.update(params[:id], params[:video])
#    video.save
    flash[:notice] = 'แก้ไขวิดีทัศน์เป็นที่เรียบร้อยแล้ว'
    redirect_to :action=>'video', :id=>video.course_id
  end
  def delete_video
    video= Video.find params[:id]
    course= video.course_id
    video.destroy
    flash[:notice]= "ลบสื่อการสอน #{video.name} เรียบร้อยแล้ว"
    redirect_to :action=>'video', :id=>course
  end
end
